// pages/tool/tool.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        tool: ['做方案', '做卡券', '做商城', 'API对接'],
        recommendlist: [],
        status: '',   // 0不能分享   1可分享
        showtips: false,
        showDownloadType: false,
        shareInfo: {},
        showShare: false,
        shareTitle: '',
        myinfo: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // app.func.onPageLoad(this, options);
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.myinfos();
        this.planLog();
    },
    recommending(){
        wx.navigateTo({
          url: '/tools/pages/tool/recommending/title/title?log_id=0',
        })
    },
    myinfos() {
        app.func.getPromise('/my/info')
            .then(([code, res]) => {
            this.setData({
                myinfo: res.message
            });
        })
    },
    planLog() {
        app.func.getPromise('/plan/log')
            .then(([code, res]) => {
                this.setData({recommendlist: res.data});
            })
    },


    // recommending: function (e) {
    //     var index = e.currentTarget.dataset.index;
    //     if (index == 0) {
    //         console.log('1111')
    //         this.checkLogin(index);
    //     } else if (index == 1) {
    //         console.log('222')
    //         this.checkLogin(index);
    //     } else if (index == 2) {
    //         console.log(333)
    //         var src = 'https://www.dianlinet.com/dianlinet/shop.html';
    //         wx.navigateTo({
    //             url: '/pages/auth/agreement/agreement?src=' + src
    //         })
    //     } else if (index == 3) {
    //         console.log('444')
    //         var src = 'https://www.dianlinet.com/dianlinet/api.html';
    //         wx.navigateTo({
    //             url: '/pages/auth/agreement/agreement?src=' + src
    //         })
    //     }
    // },


    checkLogin: function (index) {
        app.func.getuserinfo(() => {
            this.myinfo(index);
        }, () => {
            this.login();
        })
    },
    myinfo: function (index) {
        app.func.getPromise('/my/info')
            .then(([code, res]) => {
                if (code == 200) {
                    if (res.message.type == 1) {
                        this.goon(index);
                    } else {
                        this.login();
                    }
                }
            })
    },
    goon: function (index) {
        if (index == 0) {
            wx.navigateTo({
                url: '/tools/pages/tool/recommending/title/title?log_id=0'
            })
        } else if (index == 1) {
            wx.navigateTo({
                url: '/tools/pages/tool/card/make/make'
            })
        }
    },
    login: function () {
        wx.switchTab({
            url: '/pages/about/about'
        })
    },


    delete_logid: function (e) {
        var log_id = e.currentTarget.dataset.log_id;
        var that = this;
        wx.showModal({
            title: '提示',
            content: '记录一旦删除，不可恢复',
            success: function (res) {
                if (res.confirm) {
                    app.func.getPromise('/plan/dellog/' + log_id)
                        .then(([code, res]) => {
                            console.log(res)
                            if (code == 200) {
                                that.onShow();
                            }else{
                              wx.showToast({
                                title: res.message,
                                icon:'none'

                              })
                            }
                        })
                } else {
                    console.log('用户点击取消')
                }
            }
        })

    },


    addrecommend: function () {
        wx.navigateTo({
            url: '/tools/pages/tool/recommending/title/title?log_id=0'
        })
    },

    modifyInfo: function (e) {
        let log_id = e.currentTarget.dataset.log_id;
        wx.navigateTo({
            url: '/tools/pages/tool/recommending/title/title?log_id=' + log_id
        })
    },


    view_recom: function (e) {
        let log_id = e.currentTarget.dataset.log_id;
        let type = e.currentTarget.dataset.type;
        let userType = this.data.myinfo.type;
        if(userType == 0 || userType==2){
           wx.navigateTo({
               url:`/tools/pages/tool/share/share?log_id=${log_id}`
           })
        }else{
            wx.navigateTo({
                url: `/tools/pages/tool/recommending/recommending?log_id=${log_id}&type=${type}`
            })
        }
    },


    unshare: function () {
        app.func.toastPromise('方案没有制作完成')
    },

    share: function (e) {
        let id = e.currentTarget.dataset.id;
        let link = e.currentTarget.dataset.link;
        let pre_link = e.currentTarget.dataset.pre_link;
        this.setData({
            shareid: id,
            netSrc: pre_link.length > 0 ? pre_link : link,
            imgSrc: `${app.globalData.domain}/cs/plan/poster/${id}`,
            showtips: true,
            showDownloadType: true
        });
       
    },
    onShowtips() {
        this.setData({showtips: false});
    },
    cancelShare() {
        this.setData({showShare: false});
    },


    getInputValue(e) {
        this.setData({
            shareTitle: e.detail.value
        });
    },
    showShare(e) {
        let item = e.currentTarget.dataset.item;
        this.setData({
            shareInfo: item,
            shareTitle: item.title + ',请查收！',
            showShare: true,
        });
    },

    /**
     * 用户点击立即送出
     */
    onShareAppMessage: function () {
        return {
            title: this.data.shareTitle,
            imageUrl: `https://files.dianlinet.com/dianli/images/2020-10-08/15-47-23.jpg`,
            path: `/tools/pages/tool/share/share?log_id=${this.data.shareInfo.log_id}`
        }
    }

})
